<template>
  <vab-dialog v-model="dialogFormVisible" append-to-body :title="title" width="500px" @close="close">
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="审核人员姓名" prop="accountId">
        <el-select v-model="form.accountId" filterable placeholder="请选择">
          <el-option
            v-for="item in accountList"
            :key="item.accountId"
            :label="item.accountName + ' ' + item.enName"
            :value="item.accountId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核组别" prop="departments">
        <el-cascader
          v-model="form.departments"
          clearable
          :options="departmentList"
          :props="departmentProps"
          :show-all-levels="false"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </vab-dialog>
</template>

<script lang="ts" setup>
import { getTageAccount } from '/@/api/account'
import { doVerifyEdit as doEdit, getDepartmentList } from '/@/api/sys'

defineOptions({
  name: 'VerifyEdit',
})

const emit = defineEmits(['fetch-data'])
const $baseMessage = inject<any>('$baseMessage')
const formRef = ref<any>(null)
const title = ref<string>('')
const dialogFormVisible = ref<boolean>(false)
const form = reactive<any>({
  verifyType: 0,
  accountId: '',
  departments: [],
})
const departmentProps = ref<any>({
  multiple: true,
  emitPath: false,
  value: 'departmentId',
  label: 'name',
  children: 'childrenList',
  expandTrigger: 'hover',
})

const departmentList = ref<any>([])
const accountList = ref<any>([])

const rules = reactive<any>({
  accountId: [{ required: true, trigger: 'change', message: '员工不能为空' }],
  departments: [{ required: true, trigger: 'change', message: '部门不能为空' }],
})

const showEdit = (row: any) => {
  dialogFormVisible.value = true
  console.log(row)
  nextTick(() => {
    if (!row) title.value = '添加'
    else {
      title.value = '编辑'
      Object.assign(form, row)
      if (form.departments) {
        form.departments = form.departments.split(',').map(Number)
      }
    }
  })
}

defineExpose({
  showEdit,
})

const close = () => {
  formRef.value.clearValidate()
  formRef.value.resetFields()
  emit('fetch-data')
  dialogFormVisible.value = false
}

const save = () => {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      console.log(form)
      const data = {
        id: form.id,
        accountId: form.accountId,
        departments: form.departments.join(','),
      }
      const { message }: any = await doEdit(data)
      $baseMessage(message, 'success', 'hey')
      close()
    }
  })
}

const fetchDepartmentData = async () => {
  const { data } = await getDepartmentList()
  departmentList.value = data
}

const fetchVerifyAccount = async () => {
  const { data } = await getTageAccount({ tag: 'verify' })
  accountList.value = data
}

onBeforeMount(() => {
  fetchDepartmentData()
  fetchVerifyAccount()
})
</script>
